package com.flower.core_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flower.core_service.object.entity.AuthUser;
import com.flower.core_service.object.entity.AuthUserRole;
import com.flower.core_service.mapper.AuthUserMapper;
import com.flower.core_service.service.AuthUserRoleService;
import com.flower.core_service.service.AuthUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author flower
 * @since 2022-04-06
 */
@Service
@Slf4j
public class AuthUserServiceImpl extends ServiceImpl<AuthUserMapper, AuthUser> implements AuthUserService {

    @Autowired
    AuthUserRoleService authUserRoleService;

    @Override
    public List<AuthUser> getAllUser() {
        return baseMapper.getAllUser();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteUser(String id) {
        try {
            baseMapper.deleteById(id);
            QueryWrapper<AuthUserRole> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",id);
            authUserRoleService.remove(wrapper);
        }catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.info("删除异常，用户删除失败");
            return;
        }
    }
}
